﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.DataLayer;

namespace Common.MultiLanguage
{
    public class Tool
    {
        public static IList<Dictionary> RecnikCeo;

        public static void Inicijalizuj(int jezikId)
        {
            RecnikCeo = UcitajPrevode(jezikId);
        }

        public static IList<Dictionary> UcitajPrevode (int jezikId)
        {
            using (CommonDataContext dc = new CommonDataContext())
            {
                return (from t in dc.Recniks
                        where t.JezikId == jezikId
                        select new Dictionary { Original = t.Original, Prevod = t.Prevod }).ToList();
            }
        }

        #region Prevedi meni
        private static void PrevediStavkuMenija(ToolStripMenuItem m)
        {
            try
            {
                m.Text = (from t in RecnikCeo
                         where t.Original == m.Text
                         select t).SingleOrDefault().Prevod;
            }
            catch
            {
                //u slučaju da nema prevod ne radi ništa
            }

            foreach (ToolStripItem t in m.DropDownItems)
            {
                if (t is ToolStripMenuItem)
                    PrevediStavkuMenija((ToolStripMenuItem)t);
            }
        }

        public static void PrevediMeni(MenuStrip menu)
        {
            foreach (ToolStripMenuItem m in menu.Items)
                PrevediStavkuMenija(m);
        }
        #endregion

        #region Prevedi formu
        public static void PrevediFormu(Form forma)
        {
            #region Prevedi naslov forme
            try
            {
                forma.Text = (from t in RecnikCeo
                          where t.Original == forma.Text
                          select t).SingleOrDefault().Prevod;
            }
            catch
            {
                //u slučaju da nema prevod ne radi ništa
            }
            #endregion

            PrevediKontroleNaFormi(forma.Controls);
        }

        public static void PrevediKontroleNaFormi(Control.ControlCollection controlCollection)
        {
            foreach (Control c in controlCollection)
            {
                if (c.GetType() == typeof(DevExpress.XtraGrid.GridControl)) PrevediGrid((DevExpress.XtraGrid.GridControl)c);
                if (c.GetType() == typeof(MenuStrip)) PrevediMeni((MenuStrip)c);
                if (c.GetType() == typeof(StatusStrip)) PrevediStatusStrip((StatusStrip)c);
                if (c.GetType() == typeof(DevExpress.XtraEditors.RadioGroup)) PrevediRadioGroup((DevExpress.XtraEditors.RadioGroup)c);
                
                if (!String.IsNullOrEmpty(c.Text))
                {
                    try
                    {
                        c.Text = (from t in RecnikCeo
                                  where t.Original == c.Text
                                  select t).SingleOrDefault().Prevod;
                    }
                    catch
                    {
                        //c.Text = String.Format("!{0}!", c.Name);
                    }
                }

                if (c.HasChildren) PrevediKontroleNaFormi(c.Controls);
            }
        }
        #endregion

        #region Prevedi grid
        public static void PrevediGrid(DevExpress.XtraGrid.GridControl gridControl)
        {
            foreach (DevExpress.XtraGrid.Views.Grid.GridView g in gridControl.ViewCollection)
            {
                PrevediKoloneGrida(g);
            }
        }

        public static void PrevediKoloneGrida(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn m in gridView.Columns)
            {
                if (!String.IsNullOrEmpty(m.Caption))
                {
                    try
                    {
                        m.Caption = (from t in RecnikCeo
                                     where t.Original == m.Caption
                                     select t).SingleOrDefault().Prevod;
                    }
                    catch
                    {
                        //u slučaju da nema prevod ne radi ništa
                    }
                }
            }

        }
        #endregion

        #region Prevedi status bar
        public static void PrevediStatusStrip(StatusStrip statusStrip)
        {
            foreach (ToolStripLabel s in statusStrip.Items)
            {
                if (!String.IsNullOrEmpty(s.Text))
                {
                    try
                    {
                        s.Text = (from t in RecnikCeo
                                  where t.Original == s.Text
                                  select t).SingleOrDefault().Prevod;
                    }
                    catch
                    {
                        //u slučaju da nema prevod ne radi ništa
                    }
                }
            }
        }
        #endregion

        #region Prevedi radio group
        public static void PrevediRadioGroup(DevExpress.XtraEditors.RadioGroup radioGroup)
        {
            foreach (DevExpress.XtraEditors.Controls.RadioGroupItem i in radioGroup.Properties.Items)
            {
                
                if (!String.IsNullOrEmpty(i.Description))
                {
                    try
                    {
                        i.Description = (from t in RecnikCeo
                                         where t.Original == i.Description
                                         select t).SingleOrDefault().Prevod;
                    }
                    catch
                    {
                        //u slučaju da nema prevod ne radi ništa
                    }
                }
            }
        }
        #endregion
        
    }
}
